<?php

ini_set('max_execution_time', '0');
ini_set('memory_limit','128M');
date_default_timezone_set('Asia/Chongqing');

require_once "include/DB.php";

$pdb = open_pdb();

$handle = fopen("second.csv","r") or die("can not open file !");
$data = fgetcsv($handle) or die("can not read file !");

while($data = fgetcsv($handle)){

	if(empty($data[0])){
		break;
	}

	foreach($data as $key => $value){
		$data[$key] = mb_convert_encoding($value,"UTF-8","GBK");
	}

	switch($data[3]){
		case "五星":
			$data[3] = 5;
			break;
		case "四星":
			$data[3] = 4;
			break;
		case "三星":
			$data[3] = 3;
			break;
		case "二星":
			$data[3] = 2;
			break;
		case "一星":
			$data[3] = 1;
			break;
		default:
			$data[3] = 0;
	}

	$service = array();
	for($i=60;$i<73;$i++){
		if($data[$i] == "1"){
			$service[] = $i - 58;
		}
	}

	$values = array(
		"`id`=".mysql_real_escape_string($data[0]),
		"`name`='".mysql_real_escape_string($data[1])."'",
		"`en_name`='".mysql_real_escape_string($data[2])."'",
		"`telephone`='".mysql_real_escape_string($data[15])."'",
		"`star`=".intval($data[3]),
		"`province`='".mysql_real_escape_string($data[4])."'",
		"`city`='".mysql_real_escape_string($data[5])."'",
		"`area`='".mysql_real_escape_string($data[6])."'",
		"`address`='".mysql_real_escape_string($data[7])."'",
		"`service`='".mysql_real_escape_string(implode(",",$service))."'",
		"`longitude`=".floatval($data[8]),
		"`latitude`=".floatval($data[9]),
		"`createtime`=".time(),
	);

	$sql = "INSERT INTO `myshow_hotel` SET ".implode(",",$values);
	array_shift($values);
	$sql .= " ON DUPLICATE KEY UPDATE ".implode(",",$values);
	$result = $pdb->query($sql);
	if(MDB2::isError($result)){
		var_dump($result);
		exit();
	}


	$rooms = array();
	for($i=21;$i<60;$i++){
		if($data[$i] == "1"){
			$rooms[] = $i - 20;
		}
	}

	$foodfunf = array();
	for($i=73;$i<76;$i++){
		if($data[$i] == "1"){
			$foodfunf[] = $i - 71;
		}
	}
	
	$fitness = array();
	for($i=76;$i<78;$i++){
		if($data[$i] == "1"){
			$fitness[] = $i - 74;
		}
	}

	$location = array(
		'airportA' => array('name' => "",'distance' => "",'way' => ""),
        'airportB' => array('name' => "",'distance' => "",'way' => ""),
		'railwayA' => array('name' => "",'distance' => "",'way' => ""),
        'railwayB' => array('name' => "",'distance' => "",'way' => ""),
		'centerA' => array('name' => "",'distance' => "",'way' => ""),
        'centerB' => array('name' => "",'distance' => "",'way' => ""),
	);

	$locationArray = explode(";",$data[17]);
	foreach($locationArray as $key => $value){
		$locationArray[$key] = explode("，",$value);
		switch($locationArray[$key][0]){
			case "机场":
				if(empty($location["airportA"]["name"])){
					$location["airportA"]["name"] = $locationArray[$key][1];
					$location["airportA"]["distance"] = $locationArray[$key][2];
					$location["airportA"]["way"] = $locationArray[$key][3];
				}else if(empty($location["airportB"]["name"])){
					$location["airportB"]["name"] = $locationArray[$key][1];
					$location["airportB"]["distance"] = $locationArray[$key][2];
					$location["airportB"]["way"] = $locationArray[$key][3];
				}
				break;
			case "火车站":
				if(empty($location["railwayA"]["name"])){
					$location["railwayA"]["name"] = $locationArray[$key][1];
					$location["railwayA"]["distance"] = $locationArray[$key][2];
					$location["railwayA"]["way"] = $locationArray[$key][3];
				}else if(empty($location["railwayB"]["name"])){
					$location["railwayB"]["name"] = $locationArray[$key][1];
					$location["railwayB"]["distance"] = $locationArray[$key][2];
					$location["railwayB"]["way"] = $locationArray[$key][3];
				}
				break;
			case "市中心":
				if(empty($location["centerA"]["name"])){
					$location["centerA"]["name"] = $locationArray[$key][1];
					$location["centerA"]["distance"] = $locationArray[$key][2];
					$location["centerA"]["way"] = $locationArray[$key][3];
				}else if(empty($location["centerB"]["name"])){
					$location["centerB"]["name"] = $locationArray[$key][1];
					$location["centerB"]["distance"] = $locationArray[$key][2];
					$location["centerB"]["way"] = $locationArray[$key][3];
				}
				break;
		}
	}

	$surroundings = array(
		'catering1' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'catering2' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'catering3' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'shopping1' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'shopping2' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'shopping3' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'entertainment1' => array('name' =>"",'characteristics' => "",'mileage' => ""),
		'entertainment2' => array('name' =>"",'characteristics' => "",'mileage' => ""),
        'entertainment3' => array('name' =>"",'characteristics' => "",'mileage' => ""),
	);

	$surroundingsArray = explode(";",$data[18]);
	foreach($surroundingsArray as $key => $value){

		$surroundingsArray[$key] = explode("：",$value);
		$surroundingsArray[$key][1] = explode(",",$surroundingsArray[$key][1]);
		switch($surroundingsArray[$key][0]){
			case "餐饮":
				for($i=0;$i<3 && $i<count($surroundingsArray[$key][1]);$i++){
					$surroundings["catering".($i+1)]["name"] = $surroundingsArray[$key][1][$i];
				}
				break;
			case "购物":
				for($i=0;$i<3 && $i<count($surroundingsArray[$key][1]);$i++){
					$surroundings["shopping".($i+1)]["name"] = $surroundingsArray[$key][1][$i];
				}
				break;
			case "娱乐":
				for($i=0;$i<3 && $i<count($surroundingsArray[$key][1]);$i++){
					$surroundings["entertainment".($i+1)]["name"] = $surroundingsArray[$key][1][$i];
				}
				break;
		}
	}

	$values = array(
		"`hotel_id`=".intval($data[0]),
		"`opening_time`='".mysql_real_escape_string($data[13])."'",
		"`decorate_time`='".mysql_real_escape_string($data[14])."'",
		"`foodfunf`='".mysql_real_escape_string(implode(",",$foodfunf))."'",
		"`fitness`='".mysql_real_escape_string(implode(",",$fitness))."'",
		"`rooms`='".mysql_real_escape_string(implode(",",$rooms))."'",
		"`location`='".mysql_real_escape_string(json_encode($location))."'",
		"`surroundings`='".mysql_real_escape_string(json_encode($surroundings))."'",
		"`intro`='".mysql_real_escape_string(str_replace("&nbsp;"," ",$data[12]))."'",
		"`clause`='".mysql_real_escape_string(str_replace(";","\n",str_replace("&nbsp;"," ",$data[16])))."'",
		"`m_intro`='".mysql_real_escape_string(str_replace("&nbsp;"," ",$data[12]))."'",
		"`m_clause`='".mysql_real_escape_string(str_replace(";","\n",str_replace("&nbsp;"," ",$data[16])))."'",
		"`business_area`='".mysql_real_escape_string($data[10])."'",
		"`landmark`='".mysql_real_escape_string($data[11])."'",
	);

	$sql = "INSERT INTO `myshow_hotel_info` SET ".implode(",",$values);
	array_shift($values);
	$sql .= " ON DUPLICATE KEY UPDATE ".implode(",",$values);
	$result = $pdb->query($sql);
	if(MDB2::isError($result)){
		var_dump($result);
		exit();
	}
}

fclose($handle);
echo "Import finished !";

?>